<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input clearable v-model="filters.date" placeholder="搜索"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" v-on:click="search">筛选</el-button>
				</el-form-item>

<!--				<el-form-item>-->
<!--					<el-button type="primary" @click="handleAdd" v-if="userId!=444">添加新用户</el-button>-->
<!--				</el-form-item>-->

			</el-form>

		</el-col>

		<!--列表-->
		<el-table :data="list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="user_id" label="用户id" width="100" >
			</el-table-column>
			<el-table-column prop="parent" label="公司域" width="100">
			</el-table-column>
			<el-table-column prop="before_bal" label="上下分之前金额" width="100">
			</el-table-column>
			<el-table-column prop="after_bal" label="上下分之后金额" width="200" >
			</el-table-column>
			<el-table-column prop="serial_no" label="订单号" width="200" >
			</el-table-column>
			<el-table-column prop="amount" label="上下分金额" width="200" >
			</el-table-column>
			<el-table-column prop="create_at" label="创建时间" min-width="180" >
			</el-table-column>

<!--			<el-table-column prop="status" label="状态">-->
<!--				<template slot-scope="scope">-->
<!--					<el-tag :type="statusFilter(scope.row.status)">{{scope.row.status == 1?'启用':'禁用'}} </el-tag>-->
<!--				</template>-->
<!--			</el-table-column>-->


			<el-table-column label="操作" width="300">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<el-col :span="24" class="toolbar">
			<el-pagination v-if="total != 0" layout="prev, pager, next" :current-page.sync="current" @current-change="handleCurrentChange" :page-size="limit" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

<!--		&lt;!&ndash;编辑1&ndash;&gt;-->
<!--		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">-->
<!--			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">-->
<!--				<el-form-item label="姓名" prop="truename">-->
<!--					<el-input v-model="editForm.truename" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="手机号" prop="mobile">-->
<!--					<el-input v-model="editForm.mobile" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="密码">-->
<!--					<el-input type="password" v-model="editForm.password" placeholder="*******" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="角色" prop="role_type">-->
<!--					<el-select  v-model="editForm.role_type" :formatter="formatRole2" placeholder="请选择用户角色">-->
<!--						<el-option label="管理员" :value="1"></el-option>-->
<!--						<el-option label="代理商" :value="3"></el-option>-->
<!--					</el-select>-->
<!--				</el-form-item>-->

<!--				<el-form-item label="性别">-->
<!--					<el-radio-group v-model="editForm.sex">-->
<!--						<el-radio class="radio" :label="1">男</el-radio>-->
<!--						<el-radio class="radio" :label="2">女</el-radio>-->
<!--					</el-radio-group>-->
<!--				</el-form-item>-->


<!--				<el-form-item label="启用">-->
<!--					<el-switch v-model="editForm.status"></el-switch>-->
<!--				</el-form-item>-->
<!--			</el-form>-->
<!--			<div slot="footer" class="dialog-footer">-->
<!--				<el-button @click.native="editFormVisible = false">取消</el-button>-->
<!--				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>-->
<!--			</div>-->
<!--		</el-dialog>-->

<!--		&lt;!&ndash;新增界面&ndash;&gt;-->
<!--		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">-->
<!--			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">-->
<!--				<el-form-item label="姓名" prop="truename">-->
<!--					<el-input v-model="addForm.truename" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->

<!--				<el-form-item label="手机号" prop="mobile">-->
<!--					<el-input type="text" v-model="addForm.mobile" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="密码" prop="password">-->
<!--					<el-input type="password" v-model="addForm.password" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="角色" prop="role_type">-->
<!--					<el-select v-model="addForm.role_type" placeholder="请选择用户角色">-->
<!--						<el-option label="管理员" value="1"></el-option>-->
<!--						<el-option label="代理商" value="3"></el-option>-->
<!--					</el-select>-->
<!--				</el-form-item>-->


<!--				<el-form-item label="性别"  prop="sex">-->
<!--                    <el-radio v-model="addForm.sex"  label="1">男</el-radio>-->
<!--                    <el-radio v-model="addForm.sex"  label="2">女</el-radio>-->
<!--				</el-form-item>-->
<!--			</el-form>-->
<!--			<div slot="footer" class="dialog-footer">-->
<!--				<el-button @click.native="addFormVisible = false">取消</el-button>-->
<!--				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>-->
<!--			</div>-->
<!--		</el-dialog>-->


	</section>
</template>

<script>
	import { topUpDown} from '../../api/api';

	export default {
		data() {
			return {
				host:this.GLOBAL.host,
				current:1,
				user_groups:[], // 用户组

				token:'',
				userId:'',

				password:'',
				filters: {
					date: '',
				},
				list: [],
				total: 0,
				limit:10,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					truename: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					mobile: [
						{ required: true, message: '请输入手机号', trigger: 'blur' },
						// { validator: checkPhone }
					],
					role_type: [
						{ required: true, message: '请选择角色'}
					],
					group: [
						{ required: true, message: '请选择分组'}
					]
				},
				//编辑界面数据
				editForm: {
					id: 0,
					truename: '',
					sex: -1,
					mobile: '',
					password: '',
					note: '',
					status: true,
					role_type: '',
					group:'',
					tag:[],
					keshi:[],
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					truename: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					mobile: [
						{ required: true, message: '请输入手机号', trigger: 'blur' },
						// { validator: checkPhone }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					],
					role_type: [
						{ required: true, message: '请选择角色', trigger: 'blur' }
					],
					group: [
						{ required: true, message: '请选择分组'}
					],
					sex: [
						{ required: true, message: '请选择性别', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					truename: '',
					sex: -1,
					mobile: '',
					password: '',
					note: '',
					role_type: '',
					group: 0,
					keshi:[]
				}

			}
		},
		methods: {
			deltag(key){
				this.checkTags.splice(key,1);
			},
			//性别显示转换
			statusFilter(status) {
				if(status == 1){
					return 'success';
				}else if(status == 0){
					return 'danger';
				}
			},
			userStatusFilter(status) {
				if(status == 1){
					return '启用';
				}else if(status == 0){
					return '禁用';
				}
			},
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 2 ? '女' : '未知';
			},
			formatAudit: function (row, column) {
				if(row.audit == 1){
					return '认证通过';
				}else if(row.audit == 2){
					return '待审核';
				}else if(row.audit == 3){
					return '待上传';
				}else if(row.audit == 4){
					return '审核不通过';
				}
			},
			formatRole: function (row, column) {
				if(row.role_type == 1){
					return '管理';
				}else if(row.role_type == 3){
					return '代理商';
				}
			},
			formatRole2: function (role_type) {
				if(role_type == 1){
					return '管理';
				}else if(role_type == 3){
					return '代理商';
				}
			},
			handleCurrentChange(val) {
				this.page = val;
				this.current = val;
				this.getList();
			},
			search(){
				this.page = 1;
				this.current = 1;
				this.getList();
			},
			getList() {
				let para = {
					date:this.filters.date?this.filters.date:"",
					page: {
						num:this.page,
						size:10,
						total:0,
					}
				};

				this.listLoading = true;
				topUpDown(para).then((res) => {
					this.listLoading = false;
					let { message, code, data } = res;

					if(code != 200){
						this.$message({
							message: message?message:'请求失败',
							type: 'error'
						});
						return false;
					}

					this.total = data.page.total;
					this.list = data.list;

				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = {
						uid: row.id,
						token:this.token,
						userId:this.userId
					};
					removeUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
				this.editForm.password = null;
				this.editForm.status = Boolean(row.status);
				this.checkTags = row.tag;
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.checkTags = [];
				this.addForm = {
					truename: '',
					sex: -1,
					mobile: '',
					note: '',
					role_type: '',
					keshi:[]
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							let para = Object.assign({}, this.editForm);
							para.userId = this.userId;
							para.token = this.token;
							para.tag = this.checkTags;
							editUser(para).then((res) => {
								this.editLoading = false;

								if(res.data.code != 200){
									this.$message({
										message: res.data.msg?res.data.msg:'未更改',
										type: 'error'
									});
									return false;
								}

								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			//新增
			// addSubmit: function () {
			// 	this.$refs.addForm.validate((valid) => {
			// 		if (valid) {
			// 			this.$confirm('确认提交吗？', '提示', {}).then(() => {
			// 				//NProgress.start();
			// 				let para = Object.assign({}, this.addForm);
			// 				para.userId = this.userId;
			// 				para.token = this.token;
			// 				para.tag = this.checkTags;
			//
			// 				addUser(para).then((res) => {
			// 					if(res.data.code != 200){
			// 						this.$message({
			// 							message: res.data.msg?res.data.msg:'提交失败',
			// 							type: 'error'
			// 						});
			// 						return false;
			// 					}
			//
			// 					//NProgress.done();
			// 					this.$message({
			// 						message: '提交成功',
			// 						type: 'success'
			// 					});
			// 					this.$refs['addForm'].resetFields();
			// 					this.addFormVisible = false;
			// 					this.getUsers();
			// 				});
			// 			});
			// 		}
			// 	});
			// },
			selsChange: function (sels) {
				this.sels = sels;
			},
			toTime1(date){
				this.checkMonth = date;
			},
			exportInfo(){
				var url = this.host+'/api/user/exportInfo?word='+this.filters.name+'&role='+this.filters.role;
				window.location.href = url;
			}

		},

		mounted() {
			// var user = sessionStorage.getItem('user');
			var user = localStorage.getItem('user');
			// if (user) {
			// 	user = JSON.parse(user);
			// 	this.userId = user.id;
			// 	this.token = user.token;
			// }
			// var p = this.$route.query.p;
			// var r = this.$route.query.r;
			// var a = this.$route.query.a;
			// a ? this.filters.auth = parseInt(a):'';
			// r ? this.filters.role = parseInt(r):'';
			// p ? this.page = parseInt(p) :1;
			// p ? this.current = parseInt(p) :1;
			this.getList();
		}
	}

</script>

<style scoped>
.numbers{
	color: #409EFF;
	cursor: pointer;
}
</style>